package com.blate.dogxiao.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.blate.dogxiao.tools.ScreenSizeTools;

/**
 * @author Az
 * on 2020/11/18
 */
public class BlateIndexBar
        extends View {

    private final static String[] WORDS = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

    private Paint paint;

    private int cellWidth;
    private int cellHeight;
    Rect textBounds = new Rect();

    private int curIndex = -1;

    private OnIndexChangeListener indexChangeListener;

    public BlateIndexBar(Context context) {
        this(context, null);
    }

    public BlateIndexBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BlateIndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public BlateIndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        paint = new Paint();
        textBounds = new Rect();
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);//抗锯齿
        paint.setTextSize(ScreenSizeTools.spToPx(context, 10));//设置文字大小
        paint.setFakeBoldText(true);//设置文字粗体
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        cellWidth = getMeasuredWidth();
        cellHeight = getMeasuredHeight() / WORDS.length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < WORDS.length; i++) {
            String word = WORDS[i];
            paint.getTextBounds(word, 0, word.length(), textBounds);
            int x = (cellWidth - textBounds.width()) / 2;
            int y = i * cellHeight + (cellWidth + textBounds.width()) / 2;
            canvas.drawText(word, x, y, paint);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int y = (int) event.getY();
                int index = y / cellHeight;
                if (index >= 0 && index < WORDS.length) {
                    if (index != curIndex) {
                        curIndex = index;
                        if (indexChangeListener != null) {
                            indexChangeListener.onIndexChange(WORDS[curIndex]);
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int y1 = (int) event.getY();
                int index1 = y1 / cellHeight;
                if (index1 >= 0 && index1 < WORDS.length) {
                    if (index1 != curIndex) {
                        curIndex = index1;
                        if (indexChangeListener != null) {
                            indexChangeListener.onIndexChange(WORDS[curIndex]);
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                curIndex = -1;
                break;
            default:
        }
        return true;
    }

    public void setOnIndexChangeListener(OnIndexChangeListener indexChangeListener) {
        this.indexChangeListener = indexChangeListener;
    }

    public interface OnIndexChangeListener {
        void onIndexChange(String words);
    }

}
